package com.lynn.springboot_demo.controller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.awt.*;
import java.io.FileNotFoundException;

/**
 * @author cl
 * @Date 2020/8/6 09:24
 */
@Controller
@RequestMapping("/api/image")
public class ImageController {


    @RequestMapping("/scaleImage")
    @ResponseBody
    public String scaleImage(String imgUrl, float a) throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        String[] split = imgUrl.split("/");
        int length = split.length;
        imgUrl = split[length - 1];
        ImgUtil.scale(
                FileUtil.file(path + "static/img/" + imgUrl),
                FileUtil.file(path + "static/img/result.jpg"),
                a
        );
        return "result.jpg";
    }

    @RequestMapping("/pressText")
    @ResponseBody
    public String pressText(String imgUrl) throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        String[] split = imgUrl.split("/");
        int length = split.length;
        imgUrl = split[length - 1];
        ImgUtil.pressText(//
                FileUtil.file(path + "static/img/" + imgUrl),
                FileUtil.file(path + "static/img/result.jpg"),
                "版权所有", Color.WHITE, //文字
                new Font("黑体", Font.BOLD, 50), //字体
                0, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
                0, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
                0.8f//透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字
        );
        return "result.jpg";
    }
}
